package jee.boot.common.rest;

import jee.boot.common.basic.TreeController;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.common.utils.StringUtils;
import jee.boot.common.service.AreaService;
import jee.boot.entity.system.Area;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 行政区划RestController
 * 
 * @author zhilei.wang
 * @version 2018-09-28
 */
@RestController
@RequestMapping(value = "/area")
public class AreaRestController extends TreeController<AreaService, Area> {
    private static String AREA_ROOT_ID="51de9d95a32b11e7a93952540048a3ec";//区划的数据已经生成,这个是根节点(中国)的id
    /**
     * 树
     *
     * @param entity
     * @return
     */
    @PostMapping("/treeData")
    public ResponseMessage<List<Area>> treeData(@RequestBody Area entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity .setId(AREA_ROOT_ID);
        }
        entity.setParentIds("%"+entity.getId()+"%");
        List<Area> list = service.findList(entity);
        List<Area> tree = service.buildTree(list);
        return Result.success(tree);
    }
    /**
     * 初始化缓存
     *
     * @param entity
     * @return
     */
    @PostMapping("/initCache")
    public ResponseMessage<Area> initCache(@RequestBody Area entity) {
        service.initCache(entity);
        logger.debug("缓存行政区划数据");
        return Result.success();
    }
}